package com.block.common.util;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

import java.io.UnsupportedEncodingException;

/**
 * rsa 相关加密
 */
public class RSAUtil {

    /**
     * 16进制解密
     * @param content
     * @param priKey
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String rsaHexDecode(String content, String priKey) throws UnsupportedEncodingException {
        RSA rsa = SecureUtil.rsa(priKey, null);
        byte[] decrypt = rsa.decrypt(new String(HexUtil.decodeHex(content)), KeyType.PrivateKey);
        return new String(decrypt, "UTF-8");
    }

    public static String rsaHexBase64Decode(String content, String priKey) throws UnsupportedEncodingException {
        RSA rsa = SecureUtil.rsa(priKey, null);
        byte[] decrypt = rsa.decrypt(Base64Decoder.decode(Base64Decoder.decode(HexUtil.decodeHex(content))), KeyType.PrivateKey);
        return new String(decrypt, "UTF-8");
    }

    /**
     * 16进制加密
     * @param content
     * @param pubKey
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String rsaHexEncode(String content, String pubKey) throws UnsupportedEncodingException {
        RSA rsa = SecureUtil.rsa(null, pubKey);
        String decrypt = rsa.encryptHex(content, KeyType.PublicKey);
        return decrypt;
    }


    /**
     * BASE64进制解密
     * @param content
     * @param priKey
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String rsaBASE64Decode(String content, String priKey) throws UnsupportedEncodingException {
        RSA rsa = SecureUtil.rsa(priKey, null);
        byte[] decrypt = rsa.decrypt(Base64Decoder.decode(content), KeyType.PrivateKey);
        return new String(decrypt, "UTF-8");
    }

    /**
     * BASE64进制加密
     * @param content
     * @param pubKey
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String rsaBASE64Encode(String content, String pubKey) throws UnsupportedEncodingException {
        RSA rsa = SecureUtil.rsa(null, pubKey);
        String decrypt = rsa.encryptBase64(content, KeyType.PublicKey);
        return decrypt;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {

//        String after = "dHJYZmlIblVGX05VV0dNcXBHYVNUY1ROZ05zZ0VtVG5WdDRDdzdjMEJjc3poWW5EZkRoV01KYng0dURwRUxFeFRkcHJsWlZzazhCOTFtMW05NlFpSXdEcFZSc2ExRmJ6U3RJNXo4RUVBdEZITkRqdjJRTm83VUFsYnVSTzQtZWZFdldBVGthM2NfQWRSQ2RkeXY3azVsVU1FcjlHby1wYlF2TTYxLXQ5aldZPQ==";
//
//        String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANWiydh0mvBrWOjwgB8+FYmT5QXKECCMFvqyxoJmAYKllJwHtrifDZLp483okDcZ3Ih3RxTwIq1Mjs/FKx15gD0/eeCfh7tI9qVECvbLe8UmASjw8Z59m2jK7xxmmDbk2UKQ0nDeFYyznmhkd1GFiK6rE6pgnvZKAumNClbF+IajAgMBAAECgYEAgumehuXtjCY79YJrALoquIPIdCO+fjNaYYXr3zxi8IxG5NZVQg01ZDGvRx6cQSe33Gg7RrlghpHDcc6A/S7XRZJDR2puIB+IRFqCoh9u5oYw7zthUR520sUp92VIiBuhcEju0yf6gnzKRnyvjwnyiWfEIMwwlX05t4GEQLs5FwECQQDwx521bG3qXRRfNgDzLylDXjWpB9fKZOJjI9eABrzSxzKrHBC4L4+JVTi08Yc7aKc1Q5IKv0PDeXzFuW6kf16fAkEA4yPrudQ/nZPUZRfjeSJYo90nN+Wd55YVbIGnH68sU30T6S+RKa/dzp2dYnrngCu8omYsOMwarGGEECpW5b7NfQJAPLVX4kJwhELrvAXVv3sIoF/Ej9XWZsWA/xYuY61PNqXa9KGi5Y24VxwRrC2ptyTv49IhpGwNHeAnwYK7juY+aQJBANOELs6U1UzDTgV6Em1KS6sElbHa1InFXFv3/I5nzOpOTo1Wq2JbkD0Iz3fWpd/YWsgMR1/4yVn/UUDQfK1jWe0CQAbwNOaU3NBph5tGI+c3aT+PFp6OULpRCfKGUSAVRDHMB5itW9Pad4r+Uwis1+rvIwrB94zRijllXFhLj/tkrfg=";
//
////        System.out.println(new String(HexUtil.decodeHex(after)));
//
//        String result = rsaBASE64Decode(after, privateKey);
////
//        System.out.println(result);

        String s = RandomUtil.randomStringUpper(24);
        System.out.println(s);

    }
}
